package Test;


import org.drools.KnowledgeBase;
import org.drools.KnowledgeBaseFactory;
import org.drools.builder.KnowledgeBuilder;
import org.drools.builder.KnowledgeBuilderError;
import org.drools.builder.KnowledgeBuilderErrors;
import org.drools.builder.KnowledgeBuilderFactory;
import org.drools.builder.ResourceType;
import org.drools.io.ResourceFactory;
import org.drools.logger.KnowledgeRuntimeLogger;
import org.drools.logger.KnowledgeRuntimeLoggerFactory;
import org.drools.runtime.StatefulKnowledgeSession;

import CCancer.*;

/**
 * This is a sample class to launch a rule.
 */
public class CCancerTest {

    public static final void main(String[] args) {
        try {
            // load up the knowledge base
            KnowledgeBase kbase = readKnowledgeBase();
            StatefulKnowledgeSession ksession = kbase.newStatefulKnowledgeSession();
            KnowledgeRuntimeLogger logger = KnowledgeRuntimeLoggerFactory.newFileLogger(ksession, "test");
            
            VulnerableElder p=new VulnerableElder("Tom","Green","female",69);
                        
            //the symptoms of the patient
            Symptoms s1= new Symptoms();
            s1.addToSymptoms("colorectal cancer");
            s1.addToSymptoms("chronic cough");
            s1.addToSymptoms("CCancer");
            s1.addToSymptoms("episodes per year of bronchitis");
            System.out.println("Symptoms: "+s1);
            
            Diagnosis d=new Diagnosis();
            d.addToDiagnosis("colorectal cancer");
            
            
            Measure me=new Measure();
            me.addToMeasures("spacer");
            System.out.println("Measures: "+me);
                        
            Medicines m=new Medicines();
            m.addToMedicines("benzodiazepine");
            System.out.println("Medicines: "+m);
                        
           //the case Historys of the patient
            Historys h1=new Historys();
            h1.addToHistorys("two falls");
            System.out.println("Case Historys: "+h1);
                        
            //the indicators of the patient         
            Indicator I1=new Indicator("postbronchodilator forced expiratory volume in 1 second", 0.4);
            
            ksession.insert(p);
            ksession.insert(s1);
            ksession.insert(h1);
            ksession.insert(me);
            ksession.insert(I1);
            ksession.insert(d);
                       
            ksession.fireAllRules();
            logger.close();
            System.out.println(Indicator.messages);
                     
        } catch (Throwable t) {
            t.printStackTrace();
        }
    }

    private static KnowledgeBase readKnowledgeBase() throws Exception {
        KnowledgeBuilder kbuilder = KnowledgeBuilderFactory.newKnowledgeBuilder();
        kbuilder.add(ResourceFactory.newClassPathResource("CCancer.drl"), ResourceType.DRL);
       
        KnowledgeBuilderErrors errors = kbuilder.getErrors();
        if (errors.size() > 0) {
            for (KnowledgeBuilderError error: errors) {
                System.err.println(error);
            }
            throw new IllegalArgumentException("Could not parse knowledge.");
        }
        KnowledgeBase kbase = KnowledgeBaseFactory.newKnowledgeBase();
        kbase.addKnowledgePackages(kbuilder.getKnowledgePackages());
        return kbase;
    }

}
